package com.lpf.protobuf;

import com.lpf.generated.protobuf.DataInfo;

/**
 * 把对象变成字节数组，再从字节数组读取回来，还原成对象
 */
public class ProtoBufTest {

    public static void main(String args[]) throws Exception {

        // 机器A
        DataInfo.Student studentA = DataInfo.Student.newBuilder()
                                                    .setName("张三")
                                                    .setAddress("北京")
                                                    .setAge(20)
                                                    .build();

        // 转换成字节数组
        byte[] student2ByteArray = studentA.toByteArray();

        /**
         * 网络传输
         */

        // 机器B
        DataInfo.Student studentB = DataInfo.Student.parseFrom(student2ByteArray);

        System.out.println(studentB);
        System.out.println(studentB.getName());
        System.out.println(studentB.getAddress());
        System.out.println(studentB.getAge());

    }
}
